<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
    <script src="https://unpkg.com/vue-router@4"></script> 
</head>
<body>
    <div id ="app">
        <router-link to="/">主页</router-link><br>
        <router-link to="/account">用户账户</router-link>&nbsp
        <router-link to="/about">关于网站</router-link>
        <router-view></router-view>
    </div>
    <script>
        const Home={
            template:`<h2>主页信息</h2>`
        }
        const Account={
            template:`<h2>账户信息</h2>`
        }
        const About={
            template:`<h2>网站信息</h2>`
        }
const router=VueRouter.createRouter({
    history:VueRouter.createWebHashHistory(),
    routes:[
        {
            path:'/',
            component:Home
        },
        {
            path:"/account",
            name:"account",
            component:Account,
            meta:{title:"accountpage"},
            beforeEnter:(to,from,next)=>{
                const rand=Math.random()*10
                console.log(rand)
                if(rand>5.0){
                    alert('请登录')
                }else{
                    next()
                }
            }
        },
        {
            path:"/about",
            name:"about",
            component:About
        }
    ]
    
})
    router.beforeEach((to,from,next)=>{
        console.log("to",to)
        console.log("from",from)
        if(to.name!="account"&&to.name==="about"){
            console.log(1111)
            next('/account')
        }else{
            next()
        }
    })
    router.afterEach((to,from)=>{
        if(to.meta.title){
            document.title=to.meta.title
        }
    })
    const appObj=Vue.createApp({})
    appObj.use(router)
    appObj.mount("#app")
    </script>
</body>
</html>